/*
 * AgendaNotePanel.java
 *
 * Created on 15 mars 2008, 15:15
 */

package sheep.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTarget;
import org.jdesktop.swingx.JXPanel;
import sheep.controller.Workspace;
import sheep.model.graph.CyclicGraphException;
import sheep.model.graph.Graph.Direction;
import sheep.view.Component.ActionButton;
import sheep.view.Component.RoundedPanel;
import sheep.view.Component.SheepScrollbarUI;
import sheep.view.graphics2d.AnimationUtil;

/**
 *
 * @author  geek
 */
public class TaskGraphDlg extends JXPanel {
  
  //private final BackgroundMenuTitle backgroundTitle;
  private JPanel mainPanel;
  private JPanel mainPanel2;
  ///////////////////////////////////////////////////////////////////////////

  
  /** Creates new form AgendaNotePanel */
  public TaskGraphDlg(Workspace workSpace) {
    this.workSpace = workSpace;
    initComponents();
    

  }
  

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {

    MainPanel = new JXPanel();
    centerPanel = new javax.swing.JPanel();  
   
    northPanel = new javax.swing.JPanel();
    
    mainTitleLabel = new javax.swing.JLabel();
    
    cornerQuitBttn = new javax.swing.JButton();
    southPanel = new javax.swing.JPanel();
    validBttn = new ActionButton();
    quitBttn = new ActionButton();

    
    setOpaque(false);
    setLayout(new BorderLayout());
    
    
    MainPanel.setBackground(new java.awt.Color(255, 255, 255));
    MainPanel.setMaximumSize(new java.awt.Dimension(2147483647, 300));
    MainPanel.setMinimumSize(new java.awt.Dimension(73, 200));
    MainPanel.setPreferredSize(new java.awt.Dimension(100, 200));
    MainPanel.setLayout(new java.awt.BorderLayout());
    
    
    centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
    centerPanel.setOpaque(false);
    centerPanel.setLayout(new java.awt.BorderLayout());

    ///////////////////////////////
    //DESSIN PRINCIPALE
    /////////////////////////////////////////////////////////////
    
    
    
    
    
    mainPanel = new JPanel();
    mainPanel.setOpaque(false);
    BorderLayout mainPanelLayout = new BorderLayout();
    mainPanelLayout.setHgap(15);
    mainPanel.setLayout(mainPanelLayout);
    centerPanel.add(mainPanel,BorderLayout.CENTER);
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    
    
    
    mainPanel2 = new javax.swing.JPanel();
    titlePanel = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    titleTextField = new javax.swing.JTextField();
    calendrierPanel = new javax.swing.JPanel();
    calendrierLabel = new javax.swing.JLabel();
    endDatePanel1 = new javax.swing.JPanel();
    durationSpinner = new javax.swing.JSpinner();
    endDateMinLabel1 = new javax.swing.JLabel();
    beginPanel = new javax.swing.JPanel();
    beginLabel = new javax.swing.JLabel();
    beginDatePicker = new org.jdesktop.swingx.JXDatePicker();
    endPanel = new javax.swing.JPanel();
    endDatePanel = new javax.swing.JPanel();
    endDatePicker = new org.jdesktop.swingx.JXDatePicker();
    endLabel = new javax.swing.JLabel();
    commentPanel = new javax.swing.JPanel();
    commentLabel = new javax.swing.JLabel();
    commentScrollPane = new javax.swing.JScrollPane();
    commentTextArea = new javax.swing.JTextArea();

    setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 10, 1, 10));
    setOpaque(false);
    setLayout(new java.awt.BorderLayout());

    mainPanel2.setOpaque(false);
    mainPanel2.setLayout(new javax.swing.BoxLayout(mainPanel2, javax.swing.BoxLayout.PAGE_AXIS));

    titlePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    titlePanel.setOpaque(false);
    titlePanel.setLayout(new java.awt.BorderLayout());

    titleLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    titleLabel.setText("Nom");
    titleLabel.setForeground(new java.awt.Color(255, 255, 255));
    titlePanel.add(titleLabel, java.awt.BorderLayout.WEST);

    titleTextField.setFont(new java.awt.Font("Times New Roman", 1, 14));
    titleTextField.setHorizontalAlignment(javax.swing.JTextField.LEFT);
    titleTextField.setText("jTextField1");
    titleTextField.setOpaque(false);
    titleTextField.setPreferredSize(new java.awt.Dimension(120, 25));
    titleTextField.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      }
    });
    titlePanel.add(titleTextField, java.awt.BorderLayout.EAST);

    mainPanel2.add(new RoundedPanel(titlePanel));
    mainPanel2.add(Box.createVerticalStrut(10));
    
    calendrierPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    calendrierPanel.setOpaque(false);
    calendrierPanel.setLayout(new java.awt.BorderLayout());

    calendrierLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    calendrierLabel.setText("Duree");
    calendrierLabel.setForeground(new java.awt.Color(255, 255, 255));
    calendrierPanel.add(calendrierLabel, java.awt.BorderLayout.WEST);

    endDatePanel1.setOpaque(false);
    endDatePanel1.setPreferredSize(new java.awt.Dimension(120, 25));
    endDatePanel1.setLayout(new java.awt.BorderLayout(20, 0));

    durationSpinner.setFont(new java.awt.Font("Times New Roman", 1, 14));
    durationSpinner.setOpaque(false);
    durationSpinner.setPreferredSize(new java.awt.Dimension(80, 30));
    durationSpinner.addChangeListener(new ChangeListener() {

      @Override
      public void stateChanged(ChangeEvent e) {
//        Date d = beginDatePicker.getDate();
//        endDatePicker.setDateInMillis(d.getTime() + (((Integer)durationSpinner.getValue()-1) * 1000*3600*24));
//        
//        if((Integer)durationSpinner.getValue() < 1)
//          durationSpinner.setValue(1);
      }
    });
    endDatePanel1.add(durationSpinner, java.awt.BorderLayout.CENTER);

    endDateMinLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDateMinLabel1.setText("jours");
    endDateMinLabel1.setForeground(new java.awt.Color(255, 255, 255));
    endDatePanel1.add(endDateMinLabel1, java.awt.BorderLayout.WEST);

    calendrierPanel.add(endDatePanel1, java.awt.BorderLayout.EAST);

    mainPanel2.add(new RoundedPanel(calendrierPanel));
    mainPanel2.add(Box.createVerticalStrut(10));

    beginPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    beginPanel.setOpaque(false);
    beginPanel.setPreferredSize(new java.awt.Dimension(327, 38));
    beginPanel.setLayout(new java.awt.BorderLayout());

    beginLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginLabel.setText("Date de début");
    beginLabel.setForeground(new java.awt.Color(255, 255, 255));
    beginPanel.add(beginLabel, java.awt.BorderLayout.WEST);

    beginDatePicker.setFont(new java.awt.Font("Times New Roman", 1, 14));
    beginDatePicker.setPreferredSize(new java.awt.Dimension(140, 25));
    beginDatePicker.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        Date date = new Date(beginDatePicker.getDate().getTime() + ((Integer)durationSpinner.getValue()-1)*1000*3600*24);
        endDatePicker.setDate(date);
      }
    });
    beginPanel.add(beginDatePicker, java.awt.BorderLayout.EAST);

    mainPanel2.add(new RoundedPanel(beginPanel));
    mainPanel2.add(Box.createVerticalStrut(10));
    
    endPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    endPanel.setOpaque(false);
    endPanel.setPreferredSize(new java.awt.Dimension(258, 38));
    endPanel.setLayout(new java.awt.BorderLayout());

    endDatePanel.setOpaque(false);
    endDatePanel.setPreferredSize(new java.awt.Dimension(306, 20));
    endDatePanel.setLayout(new java.awt.BorderLayout());

    endDatePicker.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endDatePicker.setPreferredSize(new java.awt.Dimension(140, 25));
    endDatePicker.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        durationSpinner.setValue(
                (int)((endDatePicker.getDate().getTime() - beginDatePicker.getDate().getTime())/(1000*3600*24)) + 1);
      }
    });
    endDatePanel.add(endDatePicker, java.awt.BorderLayout.EAST);

    endPanel.add(endDatePanel, java.awt.BorderLayout.EAST);

    endLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    endLabel.setText("Date de fin");
    endLabel.setForeground(new java.awt.Color(255, 255, 255));
    endPanel.add(endLabel, java.awt.BorderLayout.WEST);

    mainPanel2.add(new RoundedPanel(endPanel));
    mainPanel2.add(Box.createVerticalStrut(10));
    
    commentPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 10, 1));
    commentPanel.setOpaque(false);
    commentPanel.setPreferredSize(new java.awt.Dimension(258, 150));
    commentPanel.setLayout(new java.awt.BorderLayout(0, 10));

    commentLabel.setFont(new java.awt.Font("Times New Roman", 1, 14));
    commentLabel.setText("Commentaires :");
    commentLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
    commentLabel.setForeground(new java.awt.Color(255, 255, 255));
    commentPanel.add(commentLabel, java.awt.BorderLayout.NORTH);

    commentScrollPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    commentScrollPane.setOpaque(false);
    commentScrollPane.setPreferredSize(new java.awt.Dimension(120, 120));

    commentTextArea.setColumns(20);
    commentTextArea.setRows(5);
    commentTextArea.setOpaque(false);
    commentTextArea.setPreferredSize(null);
    commentScrollPane.getVerticalScrollBar().setUI(new SheepScrollbarUI());
    commentScrollPane.getHorizontalScrollBar().setUI(new SheepScrollbarUI());
    commentScrollPane.setViewportView(commentTextArea);

    commentPanel.add(new RoundedPanel(commentScrollPane), java.awt.BorderLayout.CENTER);

    mainPanel2.add(new RoundedPanel(commentPanel));

    centerPanel.add(mainPanel2, java.awt.BorderLayout.CENTER);
    
    
    
    
    
    
    
    
    
    
    ////////////////////////////////////////////////////////////////////////

    
    MainPanel.add(centerPanel, java.awt.BorderLayout.CENTER);

    northPanel.setOpaque(false);
    northPanel.setLayout(new java.awt.BorderLayout());
    northPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 15, 10));
    mainTitleLabel.setFont(new java.awt.Font("Tahoma", 1, 20));
    mainTitleLabel.setForeground(new java.awt.Color(255, 255, 255));
    mainTitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    mainTitleLabel.setText("Selectionner les participants");
    northPanel.add(mainTitleLabel, java.awt.BorderLayout.CENTER);

    cornerQuitBttn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close.png"))); // NOI18N
    cornerQuitBttn.setToolTipText("Close editor");
    cornerQuitBttn.setBorder(null);
    cornerQuitBttn.setBorderPainted(false);
    cornerQuitBttn.setContentAreaFilled(false);
    cornerQuitBttn.setFocusPainted(false);
    cornerQuitBttn.setFocusable(false);
    cornerQuitBttn.setMargin(new java.awt.Insets(0, 0, 0, 0));
    cornerQuitBttn.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close-pressed.png"))); // NOI18N
    cornerQuitBttn.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/button-close-over.png"))); // NOI18N
    cornerQuitBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cornerQuitBttnActionPerformed(evt);
      }
    });
    northPanel.add(cornerQuitBttn, java.awt.BorderLayout.EAST);

    MainPanel.add(northPanel, java.awt.BorderLayout.PAGE_START);

    //southPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 1, 0, 1));
    southPanel.setOpaque(false);
    southPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 15, 5));

    validBttn.setText("   Ok    ");
    southPanel.add(validBttn);
    validBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        OKBttnActionPerformed(evt);
      }
    });
    
    quitBttn.setText("Annuler");
    quitBttn.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        quitBttnActionPerformed(evt);
      }
    });
    southPanel.add(quitBttn);

    MainPanel.add(southPanel, java.awt.BorderLayout.PAGE_END);
    MainPanel.setOpaque(true);
    MainPanel.setBackgroundPainter(new sheep.view.utils.swingx.PainterBlackCustom());
    add(new RoundedPanel(MainPanel));
  }// </editor-fold>

  private void cornerQuitBttnActionPerformed(java.awt.event.ActionEvent evt) {
    final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {}
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
}

  private void quitBttnActionPerformed(java.awt.event.ActionEvent evt) {
        final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {}
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
  }
 
    private void OKBttnActionPerformed(java.awt.event.ActionEvent evt) {
        final Container parent = this.getParent();
    Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
    fadeOut.addTarget(new TimingTarget() {
      @Override
      public void begin() {
      ////////////////////////////////////
      // RECUPERATION DES VALEURS
      ///////////////////////////////////////
      workSpace.getSelectedTask().setName(titleTextField.getText());
      
      Calendar calTemp = Calendar.getInstance();
      calTemp.setTime(beginDatePicker.getDate());
      workSpace.getSelectedTask().setActualBeginTime(calTemp, false);//.getActualBeginTime().setTime(beginDatePicker.getDate());
      calTemp.setTime(endDatePicker.getDate());
      workSpace.getSelectedTask().setActualEndTime(calTemp, false);//.getActualEndTime().setTime(endDatePicker.getDate());
      workSpace.getSelectedTask().setComment(commentTextArea.getText());
      workSpace.getSelectedTask().setDuration((Integer)durationSpinner.getValue(), true);
      //if(graph != null){
//        try {
//          workSpace.getCurrentProject().getGraph().criticalPath(true);//.getSelectedTask().getSuperTask().getGraph().criticalPath(true);//,Direction.BOTH);
//        } 
//        catch (CyclicGraphException ex) {}
      //}
      }
      @Override
      public void end() {
        setVisible2(false);
        parent.repaint();
      }
      @Override
      public void timingEvent(float f) {}
      @Override
      public void repeat() {}
    });
    fadeOut.start();
  }
    
    void setVisible2 (boolean aFlag){
    super.setVisible(aFlag);
    setEnabled(aFlag);
    setFocusable(aFlag);
    if (aFlag){
      ///////////////////////////
      //INIT
      ///////////////////////////
      mainTitleLabel.setText("Tache : " + workSpace.getSelectedTask().getName());
      titleTextField.setText(workSpace.getSelectedTask().getName());
      
      beginDatePicker.setDate(workSpace.getSelectedTask().getActualBeginTime().getTime());
      endDatePicker.setDate(workSpace.getSelectedTask().getActualEndTime().getTime());
      durationSpinner.setValue(workSpace.getSelectedTask().getDuration());
      commentTextArea.setText(workSpace.getSelectedTask().getComment());
      
      if(workSpace.getSelectedTask().isDetailled()){
        durationSpinner.setEnabled(false);
        endDatePicker.setEnabled(false);
      }
      else{
        durationSpinner.setEnabled(true);
        endDatePicker.setEnabled(true);
      }
        
    }
  }


  @Override
  public void setVisible(final boolean aFlag) {
    final Container parent = this.getParent();   
    if (!aFlag){ 
      Animator fadeOut = AnimationUtil.createFadeOutAnimation(this);
      fadeOut.addTarget(new TimingTarget() {
        @Override
        public void begin() {}
        @Override
        public void end() {
            setVisible2(false);
            parent.repaint();            
        }
        @Override
        public void timingEvent(float f) {}
        @Override
        public void repeat() {}
      });
      fadeOut.start();
    }else{
        Animator fadeIn = AnimationUtil.createFadeInAnimation(this);
        fadeIn.addTarget(new TimingTarget() {
        @Override
        public void begin() {setVisible2(true);}
        @Override
        public void end() {
          parent.repaint();
        }
        @Override
        public void timingEvent(float f) {}
        @Override
        public void repeat() {}
      });
      fadeIn.start();   
    }
  }
  
  
  // Variables declaration - do not modify
  private JXPanel MainPanel;
  private javax.swing.JPanel centerPanel;
  private javax.swing.JButton cornerQuitBttn;

  private javax.swing.JPanel northPanel;
  private javax.swing.JButton quitBttn;
  private javax.swing.JPanel southPanel;

  private javax.swing.JButton validBttn;
  
  private Workspace workSpace;

  javax.swing.JLabel mainTitleLabel;
  private javax.swing.JSpinner durationSpinner;
  private org.jdesktop.swingx.JXDatePicker beginDatePicker;
  private javax.swing.JLabel beginLabel;
  private javax.swing.JPanel beginPanel;
  private javax.swing.JLabel calendrierLabel;
  private javax.swing.JPanel calendrierPanel;
  private javax.swing.JLabel commentLabel;
  private javax.swing.JPanel commentPanel;
  private javax.swing.JScrollPane commentScrollPane;
  private javax.swing.JTextArea commentTextArea;
  private javax.swing.JLabel endDateMinLabel1;
  private javax.swing.JPanel endDatePanel;
  private javax.swing.JPanel endDatePanel1;
  private org.jdesktop.swingx.JXDatePicker endDatePicker;
  private javax.swing.JLabel endLabel;
  private javax.swing.JPanel endPanel;
  private javax.swing.JLabel titleLabel;
  private javax.swing.JPanel titlePanel;
  private javax.swing.JTextField titleTextField;
  
  
  // End of variables declaration
}



